<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-button type="primary" size="small" @click="handleOpenAddDialog(1, '0')">添加权限</el-button>
        <el-table border stripe style="margin-top: 10px" default-expand-all row-key="id" :data="permTreeData">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" type="text" @click="handleOpenAddDialog(2, row.id)">
                添加权限点
              </el-button>
              <el-button type="text" @click="handleOpenEditDialog(row.id)">编辑</el-button>
              <el-button type="text" @click="handleDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增弹层 -->
      <el-dialog title="新增权限点" :visible="showAddDialog" @close="handleAddClose">
        <el-form label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="permDorm.name" style="60%" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permDorm.code" style="60%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permDorm.description" style="60%" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permDorm.enVisible"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleAddClose">取消</el-button>
          <el-button type="primary" @click="handleAdd">确定</el-button>
        </template>
      </el-dialog>
      <!-- 编辑弹框 -->
      <el-dialog title="新增权限点" :visible="showEditDialog" @close="handleEditClose">
        <el-form label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="permDorm.name" style="60%" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permDorm.code" style="60%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permDorm.description" style="60%" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permDorm.enVisible"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleEditClose">取消</el-button>
          <el-button type="primary" @click="handleEdit">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermListAPI, addPermAPI, getPermDetailAPI, editPermAPI, delPermAPI } from '@/api/permissions'
import { arr2Tree } from '@/utils/shift'
export default {
  name: 'PermissionView',
  data () {
    return {
      // 权限树形
      permTreeData: [],
      showAddDialog: false,
      showEditDialog: false,
      // 权限表单
      permDorm: {
        name: '',
        code: '',
        descripttion: '',
        enVisible: '1'
      }

    }
  },
  created () {
    this.getPermList()
  },
  methods: {

    // 删除权限
    async handleDel (id) {
      // 默认的不让删除，所以抛出报错
      try {
        await delPermAPI(id)
        this.getPermList()
      } catch (error) {
        this.$message.error(error.message || '删除失败！')
      }
    },
    // 编辑权限
    async handleEdit () {
      // 注意：建议在调用所有api接口函数的时候，都主动捕捉错误
      // 用 try-catch 语句块去捕获，可以保证代码的健壮性
      try {
        // 校验省略
        // 发起编辑请求
        await editPermAPI(this.permDorm)
        // 关闭弹框
        this.handleEditClose()
        // 发请求获取权限列表
        this.getPermList()
      } catch (error) {
        this.$message.error(error.message || '操作失败')
      }
    },
    // 编辑权限弹窗打开
    async handleOpenEditDialog (id) {
      // 显示弹框
      this.showEditDialog = true
      // 调接口
      const resp = await getPermDetailAPI(id)
      // 赋值做回显
      this.permDorm = resp.data
    },
    // 编辑权限弹窗关闭
    handleEditClose () {
      this.showEditDialog = false
    },
    // 新增权限弹框打开时
    handleOpenAddDialog (type, pid) {
      this.showAddDialog = true
      // 保存type
      // type:1 -> 页面访问权 2 -> 按钮操作权
      // pid:父级id,'0'表示页面访问权限，否则就是按钮操作权
      // 保存type 和 pid,给handleAdd用
      this.type = type
      this.pid = pid
      // 显示弹框
      this.showAddDialog = true
    },
    // 获取权限列表
    async getPermList () {
      // 发起获取列表的请求
      const resp = await getPermListAPI()
      // console.log(resp)
      // 把列表转为树形
      this.permTreeData = arr2Tree(resp.data, '0')
      // console.log(this.permTreeData)
    },
    // 关闭新增权限弹框
    handleAddClose () {
      this.showAddDialog = false
    },
    async handleAdd () {
      // 先整体校验，省略
      // 发起新增请求
      await addPermAPI(
        {
          ...this.permDorm,
          type: this.type,
          pid: this.pid
        })
      // 关闭弹框
      this.handleAddClose()
      // 获取权限列表
      this.getPermList()
    }

  }
}
</script>

<style>
</style>
